﻿#include "dayitem.h"

DayItem::DayItem(QWidget *parent):
    QWidget(parent)
{
    setAttribute(Qt::WA_DeleteOnClose, true);
    setFixedSize(55, 55);
    setCursor(Qt::PointingHandCursor);
    setObjectName("DayItem");
    m_isSelected = false;
    m_bInMonth = CUR_MONTH;

    //加班标志
    m_jiaban = new QLabel(QStringLiteral("班"), this);
    m_jiaban->setFixedSize(15, 15);
    m_jiaban->move(0, 1);

    //休息标志
    m_xiuxi = new QLabel(QStringLiteral("休"), this);
    m_xiuxi->setFixedSize(15, 15);
    m_xiuxi->move(0, 1);

    //阳历标志
    m_dayLabel = new QLabel(this);
    m_dayLabel->setAlignment(Qt::AlignCenter);
    m_dayLabel->setFixedSize(54, 22);
    m_dayLabel->move(0, 13);

    //农历标志
    m_lunarLabel = new QLabel(this);
    m_lunarLabel->setAlignment(Qt::AlignCenter);
    m_lunarLabel->setFixedSize(54, 16);
    m_lunarLabel->move(0, 33);

    Init();

}

void DayItem::Init()
{
    m_isSelected = false;
    m_bInMonth = CUR_MONTH;

    setSelected(false);

    //加班标志
    m_jiaban->setStyleSheet(".QLabel{\ncolor:#fff;\nbackground-color:#969799;\nline-height:14px;\nfont-family: arial;\nfont-size:13px;\n}");
    m_jiaban->setVisible(false);

    //休息标志
    m_xiuxi->setStyleSheet(".QLabel{\ncolor:#fff;\nbackground-color:#f43;\nline-height:14px;\nfont-family: arial;\nfont-size:13px;\n}");
    m_xiuxi->setVisible(false);

    //阳历标志
    m_dayLabel->setStyleSheet(".QLabel{\n	font-size:18px;\n	color:#000;\n	font-family: arial;\n}");

    //农历标志
    m_lunarLabel->setStyleSheet(".QLabel{\n	color:#999;\n	font-size:12px;\n	font-family: arial;\n}");

    setStyleSheet(".DayItem{\nborder-top: 1px solid #c8cacc;\nbackground-color:#fff;\n}\n.DayItem::hover{\nborder: 3px solid #fb0;\n}");
}

void DayItem::mouseReleaseEvent(QMouseEvent* event)
{
    emit myClicked(this);
    QWidget::mouseReleaseEvent(event);
}

void DayItem::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void DayItem::setDayData(int day, QString cnDay)
{
    m_day = day;
    m_lunar = cnDay;
    m_dayLabel->setText(QString::number(day));
    m_lunarLabel->setText(cnDay);
}

void DayItem::setSelected(bool isSelected)
{
    m_isSelected = isSelected;
    if(isSelected)
    {
        QString style = styleSheet();
        setStyleSheet(style + ".DayItem{\nborder: 3px solid #fb0;\n}");
    }
    else
    {
        QString style = styleSheet();
        if(style.contains(".DayItem{\nborder: 3px solid #fb0;\n}"))
        {
            style.remove(".DayItem{\nborder: 3px solid #fb0;\n}");
            setStyleSheet(style);
        }
    }
}

bool DayItem::parse(QByteArray data)
{
    return false;
}

void DayItem::setDayState(DayState state, bool isWeekend, DayPos bInMonth, bool isHoliday, bool isToday)
{
    m_bInMonth = bInMonth;
    m_xiuxi->setVisible(state == XIUXI);
    m_jiaban->setVisible(state == JIABAN);

    if(bInMonth != CUR_MONTH)
    {
        m_dayLabel->setStyleSheet(".QLabel{\n	font-size:18px;\n	color:#bfbfbf;\n	font-family: arial;\n}");
        m_lunarLabel->setStyleSheet(".QLabel{\n	color:#bfbfbf;\n	font-size:12px;\n	font-family: arial;\n}");
        if(state == XIUXI)
        {
            setStyleSheet(".DayItem{\nborder-top: 1px solid #fff0f0;\nbackground-color:#fff;\n}\n.DayItem::hover{\nborder: 3px solid #fb0;\n}");
        }
        else if(state == JIABAN)
        {
            setStyleSheet(".DayItem{\nborder-top: 1px solid #f5f5f5;\nbackground-color:#fff;\n}\n.DayItem::hover{\nborder: 3px solid #fb0;\n}");
        }
    }
    else
    {
        if(isHoliday)
        {
            m_lunarLabel->setStyleSheet(".QLabel{\n	color:#e02d2d;\n	font-size:12px;\n	font-family: arial;\n}");
        }        

        if(isWeekend && state != JIABAN)
        {
            m_dayLabel->setStyleSheet(".QLabel{\n	font-size:18px;\n	color:#e02d2d;\n	font-family: arial;\n}");
        }

        if(isToday)
        {
            setStyleSheet(".DayItem{\nborder-top: 1px solid #c8cacc;\nbackground-color:#fb0;\n}\n.DayItem::hover{\nborder: 3px solid #fb0;\n}");
            m_dayLabel->setStyleSheet(".QLabel{\n	font-size:18px;\n	color:#fff;\n	font-family: arial;\n}");
            m_lunarLabel->setStyleSheet(".QLabel{\n	color:#fff;\n	font-size:12px;\n	font-family: arial;\n}");
        }
    }
}
